<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>上传轮播图</h1>
    <div>
        <input type="text" placeholder="轮播图超链接" id="href" />
      </div>
      <div>
        <input type="text" placeholder="提示" id="alt" />
      </div>
      <div>
        <input type="file" id="bannerimg" onchange="selectImg()"/>
      </div>
      <img src="" id="img" alt="">
      <div>
        <button onclick="uploadBanner()">提交</button>
      </div>
    </body>
    <script src="/jquery.min.js"></script>
    <script>
      let url = '';
      function selectImg () {
        // 获取文件的信息，预览图片
        const bannerImg = document.querySelector('#bannerimg');
        const previewImg = document.getElementById('img');
        
        const file = bannerImg.files[0];
        const reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function() {
            url = this.result;
            previewImg.src = this.result;
        }

      }
      function uploadBanner () {
        // 调用接口 上传图片
        const href = document.getElementById('href').value;
        const alt = document.getElementById('alt').value;
        
        $.ajax({
          url:'/banner/addAction',
          method: 'post',
          data :{
            href,alt,bannerimg:url
          },
          success:(res) => {
            if(res === 'success') {
              window.history.back();
            }
          }
        });
      }
</script>
</html>